package ccgl.com.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ccgl.com.model.MDd;
import ccgl.com.model.MDdSearchItem;
import ccgl.com.model.MDdxx;
import ccgl.com.model.MUser;
import ccgl.com.server.DdglServer;
import ccgl.com.util.GsonPrint;
import ccgl.com.util.JsonHelper;

/**
 * 订单Action
 * 
 * @author Administrator
 * 
 */
public class DdglAction extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request = null;
	HttpServletResponse response = null;
	DdglServer server = new DdglServer();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.request = req;
		this.response = resp;
		req.setCharacterEncoding("UTF-8");
		String control = this.request.getParameter("control");
		if ("saveDdxx".equals(control)) {
			addOrUpdataDdxx();
		} else if ("checkDdh".equals(control)) {
			checkDdh();
		} else if ("getDdList".equals(control)) {
			getDdList();
		} else if ("doView".equals(control)) {
			doView();
		} else if ("doEdit".equals(control)) {
			doEdit();
		} else if ("doDelete".equals(control)) {
			doDelete();
		} else if ("getDdSearchList".equals(control)) {
			getDdSearchList();
		} else if ("getDdxxList".equals(control)) {
			getDdxxList();
		} else if ("saveDdxxxx".equals(control)) {
			saveDdxxxx();
		} else if ("deleteDdxxxx".equals(control)) {
			deleteDdxxxx();
		} else {
			GsonPrint.printFormatGson("没有请求的control！", this.response);
		}
	}

	private void deleteDdxxxx() {
		String ids = this.request.getParameter("id");
		try {
			if (server.deleteDdxxxx(ids)) {
				GsonPrint.printFormatGson("删除成功", this.response);
			} else {
				GsonPrint.printFormatGson("删除失败", this.response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void saveDdxxxx() {
		MDdxx ddxx = null;
		try {
			ddxx = JsonHelper.getObjectByServletRequest(request, MDdxx.class);
			if (ddxx == null) {
				GsonPrint.printFormatGson("提交数据错误！", this.response);
				return;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			ddxx.setGxsj(new Date());
			if (server.saveOrUpdateDdxxxx(ddxx)) {
				GsonPrint.printFormatGson("保存成功", this.response);
			} else {
				GsonPrint.printFormatGson("保存出错", this.response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void getDdxxList() {
		int page = parseInt(this.request.getParameter("page"));// 当前页数
		int pagesize = parseInt(this.request.getParameter("pagesize"));// 当前页数的大小
		MUser user = (MUser) this.request.getSession().getAttribute("user");
		int star = (page - 1) * pagesize;// 从0开始
		int end = page * pagesize;
		String ddid = this.request.getParameter("ddid");
		try {
			if (user != null) {
				GsonPrint.printGson(server.getDdxxList(star, end, ddid),
						response);
			} else {
				this.request.setAttribute("error", "请登录！");
				this.request.getRequestDispatcher("/login.jsp").forward(
						request, response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void getDdSearchList() {
		MDdSearchItem item = new MDdSearchItem();
		item.setDdsjEnd(this.request.getParameter("ddsjEnd"));
		item.setDdsjStar(this.request.getParameter("ddsjStar"));
		item.setPage(parseInt(this.request.getParameter("page")));
		item.setPagesize(parseInt(this.request.getParameter("pagesize")));
		item.setDdh(this.request.getParameter("ddh"));
		item.setKhxm(this.request.getParameter("khxm"));
		try {
			int star = (item.getPage() - 1) * item.getPagesize();// 从0开始
			int end = item.getPage() * item.getPagesize();
			MUser user = (MUser) this.request.getSession().getAttribute("user");
			if (user != null) {
				GsonPrint.printGson(
						server.getDdListWithParams(item, star, end,
								user.getId()), this.response);
			} else {
				this.request.setAttribute("error", "请登录！");
				this.request.getRequestDispatcher("/login.jsp").forward(
						request, response);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doDelete() {
		try {
			if (server.deleteDdxxs(this.request.getParameter("id"))) {
				GsonPrint.printFormatGson("删除成功", this.response);
			} else {
				GsonPrint.printFormatGson("删除出错", this.response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 得到订单list
	private void getDdList() {
		int page = parseInt(this.request.getParameter("page"));// 当前页数
		int pagesize = parseInt(this.request.getParameter("pagesize"));// 当前页数的大小
		MUser user = (MUser) this.request.getSession().getAttribute("user");
		int star = (page - 1) * pagesize;// 从0开始
		int end = page * pagesize;
		try {
			if (user != null) {
				GsonPrint.printGson(server.getDdList(star, end, user.getId()),
						response);
			} else {
				this.request.setAttribute("error", "请登录！");
				this.request.getRequestDispatcher("/login.jsp").forward(
						request, response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 检测型号是否存在
	 */
	private void checkDdh() {
		String ddh = this.request.getParameter("ddh");
		MUser user = (MUser) this.request.getSession().getAttribute("user");
		try {

			if (user != null && server.exists(ddh, user.getId())) {
				GsonPrint.printFormatGson("true", this.response);
			} else {
				GsonPrint.printFormatGson("false", this.response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 增加货物
	 */
	private void addOrUpdataDdxx() {
		MDd dd = null;
		try {
			dd = JsonHelper.getObjectByServletRequest(this.request, MDd.class);
			if (dd == null || dd.toString() == null) {
				GsonPrint.printFormatGson("提交信息出错！", this.response);
				return;
			}
			String res = null;
			dd.setGxsj(new Date());
			if (dd.getId() == null || dd.getId().trim().length() == 0) {
				res = server.addDdxx(dd);
			} else {
				res = server.updateDdxx(dd);
			}
			GsonPrint.printFormatGson(res, this.response);
		} catch (Exception e) {
			try {
				GsonPrint.printFormatGson("提交信息转换出错！", this.response);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}

	// 编辑数据传递到编辑页面
	private void doEdit() {
		this.request.setAttribute("rowJsonStringData",
				this.request.getParameter("jsonobj"));
		try {
			this.request.getRequestDispatcher("/jsp/ddgl_xz.jsp").forward(
					this.request, this.response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 查看数据传递到页面
	private void doView() {
		this.request.setAttribute("rowJsonStringData",
				this.request.getParameter("jsonobj"));
		this.request.setAttribute("isReadOnly", "true");
		try {
			this.request.getRequestDispatcher("/jsp/ddgl_xz.jsp").forward(
					this.request, this.response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
